package org.jeecg.modules.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.meituan.sdk.MeituanClient;
import com.meituan.sdk.MeituanResponse;
import com.meituan.sdk.model.ddzh.tuangou.tuangouReceiptConsume.TuangouReceiptConsumeRequest;
import com.meituan.sdk.model.ddzh.tuangou.tuangouReceiptConsume.TuangouReceiptConsumeResponse;
import com.meituan.sdk.model.ddzh.tuangou.tuangouReceiptPrepare.TuangouReceiptPrepareResponse;
import org.apache.commons.lang3.time.DateUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.coupon.entity.Coupon;
import org.jeecg.modules.coupon.entity.MyCoupon;
import org.jeecg.modules.coupon.mapper.MyCouponMapper;
import org.jeecg.modules.coupon.service.IMyCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;

/**
 * @Description: 我的优惠券
 * @Author: jeecg-boot
 * @Date:   2024-06-28
 * @Version: V1.0
 */
@Service
public class MyCouponServiceImpl extends ServiceImpl<MyCouponMapper, MyCoupon> implements IMyCouponService {

    @Autowired
    private MeituanClient meituanClient;
    @Autowired
    private RedisUtil redisUtil;
    @Override
    public MyCoupon createMyCouponfromMeituan(LoginUser sysUser, Coupon coupon, TuangouReceiptPrepareResponse response) throws Exception{
        MyCoupon myCoupon = new MyCoupon();
        myCoupon.setCouponName(coupon.getName())
                .setDuration(coupon.getDuration())
                .setDurationLeft(coupon.getDuration().doubleValue())
                .setSysUserId(sysUser.getId())
                .setCouponType(coupon.getType())
                .setDisAmt(coupon.getDisAmt())
                .setEffectEndDate(coupon.getEffectEndDate())
                .setEffectStartDate(coupon.getEffectStartDate())
                .setOrigAmt(coupon.getOrigAmt())
                .setQuyuId(coupon.getQuyuId())
                .setUseEndTime(coupon.getUseEndTime())
                .setUseStartTime(coupon.getUseStartTime())
                .setUseStatus(0)
                .setMultipleFlag(coupon.getMultipleFlag())
                .setZixishiId(coupon.getZixishiId())
                .setReceiptTitle(response.getDealTitle())
                .setReceiptCode(response.getReceiptCode());

        myCoupon.setEndDate(DateUtils.addDays(new Date(),coupon.getDays()));
        this.baseMapper.insert(myCoupon);

        redisUtil.set("mycoupon:end:" + myCoupon.getId(), "",60*60*24*coupon.getDays());

        TuangouReceiptConsumeRequest tuangouReceiptConsumeRequest = new TuangouReceiptConsumeRequest();

        tuangouReceiptConsumeRequest.setReceiptCode(response.getReceiptCode());
        tuangouReceiptConsumeRequest.setCount(response.getCount());
        tuangouReceiptConsumeRequest.setRequestId(myCoupon.getId());
        tuangouReceiptConsumeRequest.setAppShopAccountName("zhulinfeng");
        tuangouReceiptConsumeRequest.setAppShopAccount("zhulinfeng");

        MeituanResponse<TuangouReceiptConsumeResponse> resp = meituanClient.invokeApi(tuangouReceiptConsumeRequest, "" + redisUtil.get("meituantoken"));

        if(resp.isSuccess()){
            myCoupon.setOrderId("meituan-" + resp.getTraceId());
            this.baseMapper.updateById(myCoupon);
        }else{
            redisUtil.del("mycoupon:end:" + myCoupon.getId());
            throw new Exception(resp.getMsg());
        }

        return myCoupon;
    }
}
